package com.fili.gui.dialogs.config;

import com.fili.core.params.DryParams;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 29, 2006
 * Time: 11:26:05 AM
 */
public class ConfigDialog extends JDialog implements ActionListener
{
    private SerialConfigPanel serialConfigPanel;
    private GuiConfigPanel guiConfigPanel;

    private JButton btnOK;
    private JButton btnCancel;

    public ConfigDialog(Dialog owner)
    {
        super(owner, true);
        setupStuff();
    }


    public ConfigDialog(Frame owner)
    {
        super(owner, true);
        setupStuff();
    }

    private void setupStuff()
    {
        setLayout(new BorderLayout());

        serialConfigPanel = new SerialConfigPanel();
        guiConfigPanel = new GuiConfigPanel();

        JTabbedPane tp = new JTabbedPane();
        tp.add("General", guiConfigPanel);
        tp.add("Port serial", serialConfigPanel);

        add(tp, BorderLayout.CENTER);

        JPanel pnlGrid = new JPanel(new GridLayout(1, 2, 5, 0));
        btnOK = new JButton("OK");
        btnOK.addActionListener(this);
        btnOK.setPreferredSize(new Dimension(80, 24));
        pnlGrid.add(btnOK);
        getRootPane().setDefaultButton(btnOK);

        btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(this);
        btnCancel.setPreferredSize(new Dimension(80, 24));
        pnlGrid.add(btnCancel);

        JPanel pnlBottom = new JPanel(new BorderLayout());
        pnlBottom.setBorder(new EmptyBorder(5, 5, 5, 5));
        pnlBottom.add(pnlGrid, BorderLayout.EAST);

        add(pnlBottom, BorderLayout.SOUTH);

        setSize(180, 300);
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (btnOK.equals(e.getSource()))
        {
            serialConfigPanel.saveParams();
            guiConfigPanel.saveParams();
            setVisible(false);
            DryParams.saveParams();
        }
        if (btnCancel.equals(e.getSource()))
        {
            setVisible(false);
        }
    }
}
